<html>
<head><meta charset="utf-8"><title>Do not traverse HIR to generate metadata compiler-team#392 · t-compiler/major changes · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/index.html">t-compiler/major changes</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html">Do not traverse HIR to generate metadata compiler-team#392</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220868895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/233931-t-compiler/major%20changes/topic/Do%20not%20traverse%20HIR%20to%20generate%20metadata%20compiler-team%23392/near/220868895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html#220868895">(Dec 24 2020 at 13:08)</a>:</h4>
<p>Creating the topic manually.</p>



<a name="220970555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/233931-t-compiler/major%20changes/topic/Do%20not%20traverse%20HIR%20to%20generate%20metadata%20compiler-team%23392/near/220970555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html#220970555">(Dec 26 2020 at 16:40)</a>:</h4>
<p>As long as perf allows for it, I think we should do it. <a href="https://github.com/rust-lang/rust/pull/80347#issuecomment-751052175">https://github.com/rust-lang/rust/pull/80347#issuecomment-751052175</a> already has some suggestions to resolve any potential perf problems that we encounter. Can you prep the PR in a way that makes it runnable for perf? (I'm not sure if tests need to pass for try builds to succeed)</p>



<a name="220970823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/233931-t-compiler/major%20changes/topic/Do%20not%20traverse%20HIR%20to%20generate%20metadata%20compiler-team%23392/near/220970823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html#220970823">(Dec 26 2020 at 16:48)</a>:</h4>
<p>The failing test is from polymorphization. I have no idea what is the failure. It says<br>
<code>error: test compilation failed although it shouldn't!</code> on a <code>build-fail</code> test.</p>



<a name="220970827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/233931-t-compiler/major%20changes/topic/Do%20not%20traverse%20HIR%20to%20generate%20metadata%20compiler-team%23392/near/220970827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html#220970827">(Dec 26 2020 at 16:49)</a>:</h4>
<p>Still, I don't think perf needs passing tests.</p>



<a name="220972262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/233931-t-compiler/major%20changes/topic/Do%20not%20traverse%20HIR%20to%20generate%20metadata%20compiler-team%23392/near/220972262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html#220972262">(Dec 26 2020 at 17:32)</a>:</h4>
<p>ok, I'll launch perf then</p>



<a name="220979287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/233931-t-compiler/major%20changes/topic/Do%20not%20traverse%20HIR%20to%20generate%20metadata%20compiler-team%23392/near/220979287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html#220979287">(Dec 26 2020 at 21:11)</a>:</h4>
<p>Perf says it need more work: <a href="https://perf.rust-lang.org/compare.html?start=30a42735a0ff678172f66f63aca746096a717293&amp;end=2e325a37b8ff6a4cce0abcacff6dc1b64a3cf624">https://perf.rust-lang.org/compare.html?start=30a42735a0ff678172f66f63aca746096a717293&amp;end=2e325a37b8ff6a4cce0abcacff6dc1b64a3cf624</a></p>



<a name="220979336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/233931-t-compiler/major%20changes/topic/Do%20not%20traverse%20HIR%20to%20generate%20metadata%20compiler-team%23392/near/220979336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html#220979336">(Dec 26 2020 at 21:13)</a>:</h4>
<p>One of the issues is the number of calls to <code>optimized_mir</code> query. I am confused by the different tests in the old code around MIR encoding.<br>
In which cases should MIR be encoded at all?</p>



<a name="220979402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/233931-t-compiler/major%20changes/topic/Do%20not%20traverse%20HIR%20to%20generate%20metadata%20compiler-team%23392/near/220979402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html#220979402">(Dec 26 2020 at 21:15)</a>:</h4>
<p>What is the use of the PrefetchVisitor instead of a plain loop?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">for</span><span class="w"> </span><span class="n">def_id</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">tcx</span><span class="p">.</span><span class="n">mir_keys</span><span class="p">(</span><span class="n">LOCAL_CRATE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="n">tcx</span><span class="p">.</span><span class="n">ensure</span><span class="p">().</span><span class="n">optimized_mir</span><span class="p">(</span><span class="n">def_id</span><span class="p">);;</span><span class="w"></span>
<span class="w">  </span><span class="n">tcx</span><span class="p">.</span><span class="n">ensure</span><span class="p">().</span><span class="n">promoted_mir</span><span class="p">(</span><span class="n">def_id</span><span class="p">);;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="220979463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/233931-t-compiler/major%20changes/topic/Do%20not%20traverse%20HIR%20to%20generate%20metadata%20compiler-team%23392/near/220979463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html#220979463">(Dec 26 2020 at 21:17)</a>:</h4>
<p>Why is MIR emission sometimes gated? Example in Fn impl items:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">def_id</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tcx</span><span class="p">.</span><span class="n">hir</span><span class="p">().</span><span class="n">local_def_id</span><span class="p">(</span><span class="n">impl_item</span><span class="p">.</span><span class="n">hir_id</span><span class="p">);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">generics</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tcx</span><span class="p">.</span><span class="n">generics_of</span><span class="p">(</span><span class="n">def_id</span><span class="p">.</span><span class="n">to_def_id</span><span class="p">());</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">needs_inline</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">generics</span><span class="p">.</span><span class="n">requires_monomorphization</span><span class="p">(</span><span class="n">tcx</span><span class="p">)</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">tcx</span><span class="p">.</span><span class="n">codegen_fn_attrs</span><span class="p">(</span><span class="n">def_id</span><span class="p">.</span><span class="n">to_def_id</span><span class="p">()).</span><span class="n">requests_inline</span><span class="p">();</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">is_const_fn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">sig</span><span class="p">.</span><span class="n">header</span><span class="p">.</span><span class="n">constness</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">hir</span>::<span class="n">Constness</span>::<span class="n">Const</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">emit_mir</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">needs_inline</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">is_const_fn</span><span class="p">;</span><span class="w"></span>
</code></pre></div>



<a name="220979512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/233931-t-compiler/major%20changes/topic/Do%20not%20traverse%20HIR%20to%20generate%20metadata%20compiler-team%23392/near/220979512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html#220979512">(Dec 26 2020 at 21:18)</a>:</h4>
<p>I think by default functions are compiled directly to assembly (or maybe bitcode?) and only generic functions need the MIR to be kept across crates</p>



<a name="220979530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/233931-t-compiler/major%20changes/topic/Do%20not%20traverse%20HIR%20to%20generate%20metadata%20compiler-team%23392/near/220979530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html#220979530">(Dec 26 2020 at 21:19)</a>:</h4>
<p>... but sometimes is not gated (example: trait items).</p>



<a name="220983348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/233931-t-compiler/major%20changes/topic/Do%20not%20traverse%20HIR%20to%20generate%20metadata%20compiler-team%23392/near/220983348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html#220983348">(Dec 26 2020 at 23:14)</a>:</h4>
<p>trait items (not <code>impl Trait for Type</code> items), must be available, because implementations need to instantiate them</p>



<a name="220983404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/233931-t-compiler/major%20changes/topic/Do%20not%20traverse%20HIR%20to%20generate%20metadata%20compiler-team%23392/near/220983404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html#220983404">(Dec 26 2020 at 23:16)</a>:</h4>
<p>So yea, especially for <code>optimized_mir</code>, you need to replicate the rules that prevent the emission of MIR. Maybe a smaller step to your PR would be to implement those rules as a function that is just based on <code>DefId</code> queries?</p>



<a name="234480590"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/233931-t-compiler/major%20changes/topic/Do%20not%20traverse%20HIR%20to%20generate%20metadata%20compiler-team%23392/near/234480590" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> apiraino <a href="https://rust-lang.github.io/zulip_archive/stream/233931-t-compiler/major-changes/topic/Do.20not.20traverse.20HIR.20to.20generate.20metadata.20compiler-team.23392.html#234480590">(Apr 14 2021 at 11:12)</a>:</h4>
<p>MCP closed for inactivity</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>